
--属性编辑
local field = {}

local mt = ac.player.__index

local function get_attr_data(self,name,class,value)
	local name_type = 0
	if name:sub(-1, -1) == '%' then
		name = name:sub(1, -2)
		name_type = 1
	end
    
    local field_data = field[name]
	if not field_data then
		log.error(('[player]错误的属性名1:[%s][%s]'):format(self:get_name(), name))
		return
	end

	local attr = self.resource
	local key1 = name
	local key2 = name .. '%'

	if not attr[key1] then
        local value = field_data.get and field_data.get(self) or field_data.value or 0
        attr[key1] = value
        attr[key2] = 0
	end
    if class=='get' then
        if name_type==0 then
            local value = attr[key1] * (1 + attr[key2] / 100)
            if field_data.on_get then
                value = field_data.on_get(self,value)
            end
            return value
        else
            return attr[key2]
        end

    elseif class=='set' and name_type==0 then
        local old_value = attr[key1] * (1 + attr[key2] / 100)
        local f
    
        if field_data.on_set then
            --需要对变动数据进行处理
            local v1 = attr[key1]
            f = field_data.on_set(self, value, value - v1)
        end
        attr[key1] = value
        attr[key2] = 0

        if field_data.set then
            field_data.set(self, attr[key1] * (1 + attr[key2] / 100), old_value)
        end
        if f then
            f()
        end

    elseif class=='add' then
        local old_value = attr[key1] * (1 + attr[key2] / 100)

        local f
        if field_data.on_set then
            local v1 = attr[key1]
            f = field_data.on_set(self, value, value - v1)
        end
    
        if name_type==0 then
            attr[key1] = attr[key1] + value
        elseif name_type==1 then
            attr[key2] = attr[key2] + value
        end
        if field_data.set then
            field_data.set(self, attr[key1] * (1 + attr[key2] / 100), old_value)
        end
        if f then
            f()
        end
    
    end
    
end

--数据增加
function mt:add(name,value)
	get_attr_data(self,name,'add',value)
	local used
    return function ()
        if used then
            return
        end
		if self.removed then return end
        used = true
        return self:add(name, -value)
    end
end

--数据设置
function mt:set(name,value)
	get_attr_data(self,name,'set',value)
end


--数据获取
function mt:get(name)
	return get_attr_data(self,name,'get')
end

--注册属性
function register(name,...)
    local param = {...}
    local data = field[name] or {}
    data.name = name
    if #param==1 and type(param[1])=='table' then
        for k,v in pairs(param[1]) do
            data[k] = v
        end
    else
        data.value = param[1]
    end

    field[name] = data
end


register('金币',{
    set = function(self,value)
        jass.SetPlayerState(self.handle, jass.PLAYER_STATE_resources_GOLD, math.floor(value))
    end,
    on_get = function(self,value)
        return math.floor(value)
    end
})

register('木材',{
    set = function(self,value)
        jass.SetPlayerState(self.handle, jass.PLAYER_STATE_resources_LUMBER, math.floor(value))
    end,
    on_get = function(self,value)
        return math.floor(value)
    end
})

register('人口',{
    set = function(self,value)
        jass.SetPlayerState(self.handle, jass.PLAYER_STATE_resources_FOOD_USED, math.floor(value))
    end,
    on_get = function(self,value)
        return math.floor(value)
    end
})

register('人口上限',{
    set = function(self)
        jass.SetPlayerState(self.handle, jass.PLAYER_STATE_resources_FOOD_CAP, math.floor(v))
    end,
    on_get = function(self,value)
        return math.floor(value)
    end
})

register('杀敌')

return {
    register = register,
    field = field,
}